package com.cn.wanxi.service;

import com.cn.wanxi.dao.NavDao;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.util.IO;
import com.cn.wanxi.util.PATH;
import com.cn.wanxi.util.Tool;
import com.sun.deploy.util.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NavService {

    /**
     * 封装所有的导航信息到list
     *
     * @return
     */
    public List<NavModel> getNavModelListForHome() {
//        空集合：目的就是为了将所有的导航对象装入集合
        /**
         * 1.创建文件夹  创建文件
         * 2.写入数据
         * 3.读取数据
         * 4.封装数组
         */
        writeNote(PATH.NAV_FILE_NAME.getPath());
        String result = IO.readNote();
        return setNavListData(result);
    }

    private List<NavModel> setNavListData(String result) {
        List<NavModel> list = new ArrayList<>();
        String[] results = result.split(PATH.SPLIT.getPath());
        String href = results[0];
        String hrefs[] = href.split(",");
        String title = results[1];
        String titles[] = title.split(",");
        for (int i = 0; i < hrefs.length; i++) {
            NavModel model = new NavModel();
            model.setTitle(titles[i]);
            model.setHref(hrefs[i]);
            list.add(model);
        }
        return list;
    }


    private void writeNote(String fileName) {
        List<String> list = new ArrayList<>();
        NavDao navDao = new NavDao();
        String[] hrefs = navDao.getHrefs();
        String aa = Arrays.toString(hrefs);
        list.add(Tool.replace(aa));
        list.add(Tool.replace(Arrays.toString(navDao.getTitleList().toArray())));
        IO.wirteNote(fileName, list);
    }


}
